package com.terios.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.terios.AuthorizeUser;
import com.terios.conts.CommonStrs;

/**
 * AuthorizeDAO.java 2012-10-7 下午6:04:28
 * 
 * @author Wang Tengrui
 */
public class AuthorizeDAO {
	private AuthorizeUserOpenHelper authorizeUserOpenHelper;
	private SQLiteDatabase db;

	public AuthorizeDAO(Context context) {
		authorizeUserOpenHelper = new AuthorizeUserOpenHelper(context);
	}

	/**
	 * 添加授权用户信息
	 */
	public void add(AuthorizeUser authorizeUser) {
		db = authorizeUserOpenHelper.getWritableDatabase();
		db.execSQL(
				"insert into "
						+ CommonStrs.AUTHORIZE_USER_TABLE_NAME
						+ "(uid,screen_name,access_token,expires_in,usericon,expires_date) values (?,?,?,?,?,?)",
				new Object[] { authorizeUser.getUid(),
						authorizeUser.getScreen_name(),
						authorizeUser.getAccess_token(),
						authorizeUser.getExpires_in(),
						authorizeUser.getUserIcon(),
						authorizeUser.getExpires_date() });
		db.close();
	}

	/**
	 * 更新授权用户信息
	 */
	public void update(AuthorizeUser authorizeUser) {
		db = authorizeUserOpenHelper.getWritableDatabase();
		db.execSQL(
				"update "
						+ CommonStrs.AUTHORIZE_USER_TABLE_NAME
						+ " set screen_name = ?,access_token = ?,expires_in = ?,usericon = ?,expires_date = ? where uid = ?",
				new Object[] { authorizeUser.getScreen_name(),
						authorizeUser.getExpires_in(),
						authorizeUser.getUserIcon(),
						authorizeUser.getExpires_date(), authorizeUser.getUid() });
		db.close();
	}

	public void update(String uid, byte[] b) {
		db = authorizeUserOpenHelper.getWritableDatabase();
		db.execSQL("update " + CommonStrs.AUTHORIZE_USER_TABLE_NAME
				+ "set usericon = ? where uid = ?", new Object[] { b, uid });
		db.close();
	}

	/**
	 * 查找授权用户信息
	 */
	public AuthorizeUser find(String uid) {
		db = authorizeUserOpenHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select uid,screen_name,access_token,expires_in,usericon,expires_date from "
						+ CommonStrs.AUTHORIZE_USER_TABLE_NAME
						+ " where uid = ?", new String[] { uid });
		if (cursor.moveToNext()) {
			return new AuthorizeUser(uid, cursor.getString(cursor
					.getColumnIndex("screen_name")), cursor.getString(cursor
					.getColumnIndex("access_token")), cursor.getString(cursor
					.getColumnIndex("expires_in")), cursor.getBlob(cursor
					.getColumnIndex("usericon")), cursor.getString(cursor
					.getColumnIndex("expires_date")));
		}
		cursor.close();
		db.close();
		return null;
	}

	/**
	 * 查询数据库中是否存在该用户信息
	 * 
	 * @return true 存在该用户信息 false 不存在该用户信息
	 */
	public boolean findUid(String uid) {
		db = authorizeUserOpenHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select uid from "
				+ CommonStrs.AUTHORIZE_USER_TABLE_NAME + " where uid = ?",
				new String[] { uid });
		if (cursor.moveToNext()) {
			cursor.close();
			db.close();
			return true;
		} else {
			cursor.close();
			db.close();
			return false;
		}
	}

	/**
	 * 删除授权用户信息,注：该方法还没写完
	 */
	public void delete(AuthorizeUser authorizeUser) {

	}

	/**
	 * 返回授权用户list
	 */
	public List<AuthorizeUser> getAuthorizeUserList() {
		List<AuthorizeUser> authorizeUserList = new ArrayList<AuthorizeUser>();
		db = authorizeUserOpenHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CommonStrs.AUTHORIZE_USER_TABLE_NAME, new String[] {});
		while (cursor.moveToNext()) {
			authorizeUserList.add(new AuthorizeUser(cursor.getString(cursor
					.getColumnIndex("uid")), cursor.getString(cursor
					.getColumnIndex("screen_name")), cursor.getString(cursor
					.getColumnIndex("access_token")), cursor.getString(cursor
					.getColumnIndex("expires_in")), cursor.getBlob(cursor
					.getColumnIndex("userIcon")), cursor.getString(cursor
					.getColumnIndex("expires_date"))));
		}
		cursor.close();
		db.close();
		return authorizeUserList;
	}
}
